package com.itheima.mall.service.omsService;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.PageResult;
import com.itheima.mall.mapper.omsMapper.OmsOrderReturnReasonMapper;
import com.itheima.pojo.oms.OmsOrderReturnApply;
import com.itheima.pojo.oms.OmsOrderReturnReason;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

@DubboService
public class OmsOrderReturnReasonApiImpl implements OmsOrderReturnReasonApi{
    @Autowired
    private OmsOrderReturnReasonMapper omsOrderReturnReasonMapper;

    @Override
    public PageResult<OmsOrderReturnReason> findByPage(Integer pageNum, Integer pageSize) {

        PageResult<OmsOrderReturnReason> pageResult = new PageResult<>();
        Page<OmsOrderReturnReason> page = new Page<>(pageNum,pageSize);
        omsOrderReturnReasonMapper.selectPage(page,null);

        pageResult.setTotal((int)page.getTotal());
        pageResult.setTotalPage((int)page.getPages());
        pageResult.setItems(page.getRecords());
        pageResult.setPageSize(pageSize);
        pageResult.setPageNum(pageNum);

        return pageResult;
    }

    @Override
    public void deleteReturnReason(Integer[] ids) {
        for (Integer id : ids) {
            OmsOrderReturnReason omsOrderReturnReason = omsOrderReturnReasonMapper.selectById(id);
            if (omsOrderReturnReason.getStatus()==1) {
               throw new RuntimeException("选中的原因中有状态为有用,不可删除");
            }
            omsOrderReturnReasonMapper.deleteById(id);

        }
    }

    @Override
    public void updateStatus(Integer[] ids, Integer status) {

        LambdaUpdateWrapper<OmsOrderReturnReason> wrapper = new LambdaUpdateWrapper<>();
        wrapper.in(OmsOrderReturnReason::getId,ids).set(OmsOrderReturnReason::getStatus,status);
        omsOrderReturnReasonMapper.update(null,wrapper);

    }

    @Override
    public OmsOrderReturnReason getById(Integer id) {

        return  omsOrderReturnReasonMapper.selectById(id);
    }

    @Override
    public void updateById(Integer id, OmsOrderReturnReason orderReturnReason) {
        orderReturnReason.setId(id.longValue());
        omsOrderReturnReasonMapper.updateById(orderReturnReason);
    }

    @Override
    public void save(OmsOrderReturnReason omsOrderReturnReason) {

        omsOrderReturnReasonMapper.insert(omsOrderReturnReason);
    }
}
